package com.livegoods.service;

import com.livegoods.pojo.House;
import com.livegoods.pojo.LivegoodsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DetailsServiceImpl implements DetailsService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Value("${livegoods.fastdfs.nginx}")
    private String nginx;

    @Cacheable(cacheNames = "livegoods",key = "#id")
    public House details(String id) {
        System.out.println("id = " + id);
        Query query = new Query(Criteria.where("id").is(id));
        House house = mongoTemplate.findOne(query, House.class);
        List<String> imgs = house.getImgs();
        for (int i = 0; i < imgs.size(); i++) {
            imgs.set(i,nginx+imgs.get(i));
        }
        house.setImgs(imgs);

        return house;
    }

    @Override
    public Map<String,Object> buytime(String id) {

        House house = mongoTemplate.findOne(new Query(Criteria.where("id").is(id)), House.class);

        try{

            HashMap<String, Object> resultMap = new HashMap<>();
            resultMap.put("status",200);
            resultMap.put("time",house.getBuytime().getTime());
            return resultMap;
        }catch (Exception e){
            HashMap<String, Object> resultMap = new HashMap<>();
            resultMap.put("status",500);
            resultMap.put("msg","获取购买时间失败,请联系管理员");
            return resultMap;
        }
    }
}

